# -*- coding: utf8 -*-
import logging
import re
import time
import common
from urllib import unquote
from xml.dom.minidom import parseString

site_index = 'yahoo'
site_keyword = 'yahoo'
site_url = 'http://music.yahoo.co.jp/lyrics/'
test_url = 'http://music.yahoo.co.jp/lyrics/dtl/KAA013487/AAA108388/'

class YahooMusic:
    def __init__(self):
        self.lyric = None
        self.song_info = None
        self.cookie = None
        self.userAgent = 'Opera/9.80 Presto/2.5.22 Version/10.50\r\nX-AGE-Agent:'
        self.headers = {'User-Agent': self.userAgent}
        
    def get_lyric(self, url):
        if not self.get_cookie(url):
            logging.warning('%s cannot get cookie' % (url, ))
            # 1.2 try outsider PHP

            tmp = self.get_lyric_using_csie(url)

            if not tmp:
                return 'Cannot get cookie (due to limitation of Google App Engine)'
            else:
                return tmp
            

        # 2. request lyric page again to get xmlpath
        xmlpath = self.get_xmlpath(url)
        if not xmlpath:
            return 'Cannot get xmlpath'
        logging.debug('xmlpath: %s' % (xmlpath, ))

        # 3. get xml
        xml_str = self.get_xml(xmlpath)
        if not xml_str:
            return 'Cannot get xml'
        logging.debug('xml_str: %s' % (xml_str, ))

        # 4. parse xml
        lyric = self.parse_xml(xml_str)

        return lyric

    def get_cookie(self, url):
        req = common.URL(url, data=None, headers=self.headers)
        info = req.get_info()

        if 'Set-Cookie' not in info:
            logging.debug('no Set-Cookie, info:%s' % (repr(info)))
            return None

        self.headers['Cookie'] = info['Set-Cookie']
        return self.headers['Cookie']

    def get_lyric_using_csie(self, lyric_url):
        csie_url = 'http://w.csie.org/~b91072/php/lyric_get_helper/site_yahoo.php'

        logging.warning('csie_url:[%s]' % (csie_url,))

        url = '%s?time=%.0f&url=%s' % (csie_url, time.time(), lyric_url)

        bytes = common.get_url_content(url)
        logging.warning('count of bytes: %d' % (len(bytes), ))
        lyric = bytes.decode('utf-8', 'ignore').strip()

        return lyric

    def get_xmlpath(self, url):
        bytes = common.get_url_content(url, headers=self.headers)

        pattern = "xmlpath +: +'([^']+)'"
        xmlpath = common.get_first_group_by_pattern(bytes, pattern)

        return xmlpath

    def get_xml(self, xmlpath):
        if xmlpath.find('http://') == -1:
            xmlpath = 'http://music.yahoo.co.jp%s' % (unquote(xmlpath))
        logging.debug('url:%s' % (xmlpath, ))
        bytes = common.get_url_content(xmlpath, headers=self.headers)

        return bytes

    def parse_xml(self, xml_str):
        doc = parseString(xml_str)

        def xml_get_name_attribute(doc, tagname):
            ret = None
            elements = doc.getElementsByTagName(tagname)
            if elements.length == 1:
                ret = elements.item(0).getAttribute('name')
            return ret

        def xml_get_first_child(doc, tagname):
            ret = None
            elements = doc.getElementsByTagName(tagname)
            if elements.length == 1:
                ret = elements.item(0).firstChild.nodeValue
            return ret

        def get_lyric(doc):
            lyric = xml_get_first_child(doc, 'AllLyrics')
            lyric = unicode(lyric) 
            lyric = lyric.replace('<br>', '\r\n')

            return lyric

        def get_song_info(doc):
            artist = xml_get_name_attribute(doc, 'Artist')
            title = xml_get_name_attribute(doc, 'Lyrics')
            lyric = xml_get_first_child(doc, 'LyricsWriter')
            music = xml_get_first_child(doc, 'LyricsComposer')

            lines = []
            lines.append(u'%s\n' % (title))
            lines.append(u'歌手：%s' % (artist))
            lines.append(u'作詞：%s' % (lyric))
            lines.append(u'作曲：%s' % (music))

            lines.append('\n\n')

            string = '\n'.join(lines)
            return string

        return get_song_info(doc) + get_lyric(doc)

def get_lyric(url):
    js = YahooMusic()

    lyric = js.get_lyric(url)

    return lyric

def _get_song_info(url):
    pass

if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    url = test_url
    print url
    lyric = get_lyric(url)
    print repr(lyric) 


